<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>
          产品管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-select v-model="ser.categoryId" class="handle-select mr10">
          <el-option :key="0" label="全部" :value="0"></el-option>
          <el-option :key="it.id" :label="it.name" :value="it.id" v-for="it in types"></el-option>
          <template #prefix>产品类型</template>
        </el-select>
        <el-input v-model="ser.key" placeholder="产品名称" style="width: 260px" class="handle-input mr10" @keypress.enter="searchClick" clearable>
          <template #prepend>产品名称</template>
        </el-input>

        <el-button type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
        <el-button @click="addClick">添加</el-button>
      </div>
      <el-table :data="datas" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="categoryName" label="产品类别"></el-table-column>
        <el-table-column prop="name" label="产品名称"></el-table-column>
        <el-table-column prop="code" label="产品编码"></el-table-column>
        <el-table-column prop="money" label="单价" v-if="bigScreen" :formatter="priceformatter"></el-table-column>
        <el-table-column prop="spec" label="规格" v-if="bigScreen"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="ser.page" :page-size="ser.row" :total="total" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>
    <form-add ref="r_add" @search="search"></form-add>
  </div>
</template>
<script>
import FormAdd from './Add.vue'
export default {
  components: { FormAdd },
  data() {
    return {
      ser: {
        categoryId: 0,
        key: '',
        page: 1,
        row: 10,
      },
      types: [],
      datas: [],
      total: 0,
      bigScreen: window.innerWidth > 768,
    }
  },
  methods: {
    addClick() {
      this.$refs.r_add.show()
    },
    searchClick() {
      this.handlePageChange(1)
    },
    handlePageChange(page) {
      this.ser.page = page
      this.search()
    },
    search() {
      this.$api.product.search(this.ser).then(({ msg: { items, total } }) => {
        this.datas = items
        this.total = total
      })
    },
    handleEdit(row) {
      this.$refs.r_add.show(row)
    },
    handleDelete(row) {
      this.$confirm('是否确认删除当前产品？')
        .then(() => {
          this.$api.product.remove(row.id).then(({ state, msg }) => {
            this.$message[state ? 'success' : 'warning'](msg)
            if (state) this.search()
          })
        })
        .catch(() => {})
    },
    priceformatter(...arr) {
      const unit = arr[0].unit
      return arr[2] + '元' + (unit ? `/${unit}` : '')
    },
  },
  mounted() {
    this.searchClick()
    this.$api.dictionary.enums(6).then(({ msg }) => {
      this.types = msg
    })
  },
}
</script>
<style lang="scss" scoped></style>
